import { _decorator, Component, Vec3, tween, Color, Sprite, find, Contact2DType, IPhysics2DContact, Collider2D, BoxCollider2D, UITransform } from 'cc';
const { ccclass } = _decorator;
import { Boss } from './Boss';
import { Monster } from './Monster';

@ccclass('Fireball')
export class Fireball extends Component {

    start() {
        this.initColliderEvents()
        this.fire()
    }

    /** 
     * @zh
     * 初始化碰撞组件。
    */
    initColliderEvents() {
        let fireballCollider = this.node.getComponent(BoxCollider2D)
        fireballCollider.on(Contact2DType.BEGIN_CONTACT, this.onFireballBeginContact, this)
    }

    /** 
     * @zh
     * 发射火球。
    */
    fire() {
        let canvasNode = find('Canvas')
        let canvasWidth = canvasNode.getComponent(UITransform).width
        let canvasHeight = canvasNode.getComponent(UITransform).height
        let fireballUITranform = this.node.getComponent(UITransform)
        
        let x = 0
        let y = 0
        let randomIndex = Math.floor(Math.random() * 2)

        // 从左到右发射
        if (randomIndex == 0) {
            this.node.setScale(2, 2, 1) // 让火球看起来大一些
            x = - canvasWidth / 2 - fireballUITranform.width
            y = Math.random() * canvasHeight - canvasHeight / 2
            tween(this.node).to(1.5, {'position': new Vec3(canvasWidth / 2 + fireballUITranform.width + 100 , y, 0)})
                            .call(()=>{this.node.removeFromParent()})
                            .start()
        }

        // 从右到左发射
        else if (randomIndex == 1) {
            this.node.setScale(-2, 2, 1)
            x = canvasWidth / 2 + fireballUITranform.width
            y = Math.random() * canvasHeight - canvasHeight / 2
            tween(this.node).to(1.5, {'position': new Vec3(-canvasWidth / 2 - fireballUITranform.width - 100 , y, 0)})
                            .call(()=>{this.node.removeFromParent()})
                            .start()
        }

        this.node.setPosition(x, y, 0)
    }

    /** 
     * @zh
     * 逐渐变得不透明。
    */
    show() {
        let color = this.node.getComponent(Sprite).color
        let newAlphaValue = color.a + 20
        this.node.getComponent(Sprite).color = new Color(color.r, color.g, color.b, newAlphaValue)
        
        if (newAlphaValue >= 255) {
            this.unschedule(this.show)
        }
    }
        
    /** 
     * @zh
     * 一开始要设置图片的alpha值为0，否则刚生成像素块时设置的十六进制颜色就会直接让计时器中的逻辑无效。
    */
    setAlphaToZero() {
        let color = this.node.getComponent(Sprite).color
        this.node.getComponent(Sprite).color = new Color(color.r, color.g, color.b, 0)
    }

    /** 
     * @zh
     * 火球碰撞逻辑。
    */
    onFireballBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact|null) {
        let damage = Math.round(Math.random() * 80 + 100)
        if (otherCollider.node.active && otherCollider.node.name == 'Boss') {
            otherCollider.node.getComponent(Boss).reduceLife(damage)
        }
        else if (otherCollider.node.name == 'monster') {
            otherCollider.node.getComponent(Monster).reduceLife(damage)
        }
    }
}


